//index.js 
//获取应用实例 
const app = getApp();
const util = require('../../../utils/util.js');
const lbbService = require('../../../pages/service/service.js');
const lbbTaskService = require('../service/task-service.js');
Page({
    data: {
        userList: [],
        propArray: [],
        marketTaskTypeList:[{taskName:'全部项目',marketTaskType:''}],
        taskName:'全部项目',
        isRoutingStaticMenus: 0,
        activeKey: 0,
        background: "#f2f2f2",
        year: '',
        month: '',
        taskType: '',
        marketTaskType: '',
        isHandle: "0",
        provinceId: "",
        hosPatrolList: [],
        maintenancetasks: [],
        longTaskList: [],
        cityId: "",
        city: '',
        taskCity: [1, 2, 3, , , , , ],
        grade: '',
        page: "1",
        loading: false,
        loaded: true,
        isBackBtn: true, // 设置是否显示回到顶部按钮
        defaultSetting: {
            shake: true, // 设置是否开启震动
            style: "black", // 设置圆点申诉还是浅色
        },
    },
    /**
     * 生命周期函数--监听页面加载
     */

    onLoad: function (e) {
        var that = this;
        that.setData({
            taskType: 1
        })
        that.getProjectList()
        that.getWorker();
    },

  //获取项目列表
  getProjectList() {
    let that = this
    lbbTaskService.getDictByCode({
        code: 'taskProject'
    }, function (res) {
        res.data.forEach(item=>{
            item.taskName=item.label
            item.marketTaskType=item.value
        })
        that.data.marketTaskTypeList.push(...res.data)
        that.setData({
            marketTaskTypeList: that.data.marketTaskTypeList
        })
    })
},
    //获取基本配置
    getBaseConfig: function () {
        let that = this;
        lbbService.getBaseConfig({}, res => {
            console.log('重制')
            util.print('config', res);
            app.globalData.config = res;
            that.setData({
                isRoutingStaticMenus: res.isRoutingStaticMenus
            })
        })
    },
    //获取师傅
    getWorker: function () {
        let that = this;
        lbbService.getWorker({
            taskType: that.data.taskType
        }, function (res) {
            console.log(89898, res)
            if (res.data.length == 1) {
                that.setData({
                    userId: res.data[0].id,
                    userName: res.data[0].name
                })
            } else {
                wx.getUserInfo({
                    success(res) {
                        that.setData({
                            userName: res.userInfo.nickName
                        })
                    }
                })
            }
            that.getTaskList()
            that.setData({
                userList: res.data
            })
        })
    },
    //获取医院任务列表
    getTaskList: function () {
        let that = this
        let date = new Date()
            lbbService.getTemporaryTaskList({
                isSelfBuild:1,
                status:that.data.activeKey==2?-1:that.data.activeKey,
                taskProject:that.data.marketTaskType,
                provinceId: that.data.provinceId,
                cityId: that.data.cityId,
                userId: that.data.userId,
                taskType: that.data.taskType,
                pageNo: that.data.page,
                pageSize: "5",
                city: that.data.city,
            }, function (res) {
                let lists = [...that.data.hosPatrolList, ...res.data.records]
                if (that.data.city && lists.length == 0) {
                    that.setData({
                        city: '',
                        grade: '',
                    })
                    that.getTaskList()
                } else {
                    that.setData({
                        loading: false,
                        loaded: true,
                        hosPatrolList: lists,
                        TotalPage: res.data.pages
                    })
                }

            })
            lbbService.cityStatistics({
                isSelfBuild:1,
                status:that.data.activeKey==2?-1:that.data.activeKey,
                taskProject:that.data.marketTaskType,
                provinceId: that.data.provinceId,
                cityId: that.data.cityId,
                userId: that.data.userId,
                taskType: that.data.taskType,
                pageNo: that.data.page,
                pageSize: "5",
                city: that.data.city,
            }, function (res) {
                console.log('有任务医院', res.data)
                that.setData({
                    taskCity: res.data
                })
            })
    },
    cityDetail: function (e) {
        console.log('525858', e.target.dataset.cityname)
        let that = this
        that.setData({
            grade: e.target.dataset.cityname,
            page: '1',
            hosPatrolList: [],
            longTaskList: [],
            maintenancetasks: [],
            city: e.target.dataset.cityname
        })
        that.getTaskList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
            this.setData({
                hosPatrolList: [],
                page: '1'
            })
            this.getTaskList();
    },
    //切换侧边导航栏
    changeStat(e) {
        let that = this
        that.setData({
            hosPatrolList: [],
        })
        if (e.detail == "0") {
            that.setData({
                isHandle: "0",
                activeKey: e.detail,
                page: '1',
                city: '',
                grade: ''
            })
            that.getTaskList()
        } else if (e.detail == "1") {
            that.setData({
                isHandle: "0",
                activeKey: e.detail,
                page: '1',
                city: '',
                grade: ''
            })
            that.getTaskList()
        } else if (e.detail == "2") {
            that.setData({
                isHandle: "0",
                activeKey: e.detail,
                page: '1',
                city: '',
                grade: ''
            })
            that.getTaskList()
        } else if (e.detail == "3") {
            that.setData({
                month: month,
                year: year,
                isHandle: "1",
                activeKey: e.detail,
                page: '1',
                city: '',
                grade: ''
            })
            that.getTaskList()
        }

    },
    //事件处理函数
    myEventListener: function (e) {
        console.log("myEventListener>>", e)
        let that = this
        that.setData({
            provinceId: e.detail[0] == null ? "" : e.detail[0],
            cityId: e.detail[1] == null || e.detail[1] == -1 ? "" : e.detail[1],
            month: e.detail[2] == null || e.detail[2] == -1 ? that.data.month : e.detail[2],
            userId: e.detail[5] == null || e.detail[5] == 0 ? that.data.userId : e.detail[5],
            marketTaskType: e.detail[6] == null || e.detail[6] == -1 ? that.data.marketTaskType : e.detail[6],
            page: '1',
            city: '',
            grade: '',
            hosPatrolList: [],
            longTaskList: [],
            maintenancetasks: []
        })
        that.getTaskList()
    },
    //上拉加载更多
    eventhandle(e) {
        let that = this
        let pagenum = Number(that.data.page)
        if (pagenum >= that.data.TotalPage) {
            that.setData({
                loading: false,
                loaded: true
            })
        } else {
            pagenum = pagenum + 1
            that.setData({
                loading: true,
                loaded: false
            })
            that.setData({
                page: pagenum.toString()
            })
            that.getTaskList()
        }
    },
    //删除任务
    delTask(e){
        let that=this
        wx.showModal({
            title: '提示',
            content: `您确定删除任务吗？`,
            showCancel: true,
            confirmText: '确定',
            success(res) {
                if (res.confirm) {
                    lbbTaskService.removeTask({taskId:e.currentTarget.dataset.taskid},function(res) {
                        // that.data.hosPatrolList.splice(e.currentTarget.dataset.index,1)
                        that.setData({
                            hosPatrolList: []
                        })
                      that.getTaskList()
                    })
                }
            }
        })   
    },
    //任务编辑
    editTask(e){
        wx.navigateTo({
            url:`/pkgTask/pages/addTaskModel/addTaskModel?taskId=${e.currentTarget.dataset.taskid}&userId=${e.currentTarget.dataset.userid}` 
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },
    //图片预览
    preViewImgs(e) {
        let current = e.currentTarget.dataset.current
        let urls = e.currentTarget.dataset.imglist
        util.previewImgs(current, urls);
    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        // wx.switchTab({
        //     url: '../hospitalList/hospitalList',
        //   })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

})